<?php
class WebThemeModel extends Model {
	var $_table = 'web_theme';
	
	var $theme_id_nak; //INT UNSIGNED NOT NULL AUTO_INCREMENT,
	var $name_sr; //VARCHAR(32) NOT NULL COMMENT '模板名称',
	var $selected_nr = 0; //TINYINT(1) UNSIGNED NOT NULL COMMENT '默认主题',
	var $note_s; //VARCHAR(256) NULL COMMENT '备注',
	
	public function check_name($value, $data) {
		$where ['name'] = $value;
		if (! empty ( $data ['theme_id'] )) {
			$where ['theme_id !='] = $data ['theme_id'];
		}
		return $this->exist ( $where ) ? "主题已经存在" : true;
	}
	
	public function setDefault($tid) {
		$t0 = array ('selected' => 0 );
		$t1 = array ('selected' => 1 );
		$selected = $this->where ( $t1 )->retrieve ( 'theme_id', 0 ); //查找当前默认主题
		if ($selected && $selected ['theme_id'] == $tid) { //当前默认主题与要设置的相同
			return true;
		} else {
			$this->begin ();
			$rst = $selected ? $this->update ( $t0, $selected ['theme_id'] ) : true;
			$rst1 = $this->update ( $t1, $tid );
			if ($rst && $rst1) { //修改数据库成功
				return $this->commit ();
			} else {
				$this->rollback ();
				return false;
			}
		}
	}
	public function deleteTheme($tid) {
		$this->begin ();
		$rst = $this->delete ( $tid );
		$rst1 = true;
		if ($rst !== false && $rst > 0) {
			$ttM = new WebTemplateModel ();
			$rst1 = $ttM->delete ( array ('theme_id' => $tid ) );
		}
		if ($rst !== false && $rst1 !== false) {
			return $this->commit ();
		}
		$this->rollback ();
		return false;
	}	
}